﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skewworks.Win.Motif.Glide
{
    public partial class GlideRadioButton : UserControl, IEmbeddedControl
    {

        private GlideRadioButtonSettings _settings;
        private bool _checked;
        private bool _showBackground;
        private string _value;
        private string _groupName;
        private Color _color;
        private Color _outline;
        private Color _selColor;
        private Color _selOutline;

        public GlideRadioButton()
        {
            InitializeComponent();
            _settings = new GlideRadioButtonSettings(this);
            this.Width = 32;
            this.Height = 32;
            _checked = false;
            _showBackground = true;
            _groupName = "radioButtonGroup";
            _color = Color.FromArgb(212, 212, 212);
            _outline = Color.FromArgb(184, 184, 184);
            _selColor = Color.FromArgb(53, 139, 246);
            _selOutline = Color.FromArgb(0, 45, 255);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //e.Graphics.DrawImage(Properties.Resources.RadioButton_uc, 0, 0, 32, 32);
            lock (this)
            {
                if (_showBackground)
                    e.Graphics.FillRectangle(new LinearGradientBrush(new Rectangle(0, 0, Width, Height), Color.White, Color.FromArgb(210, 210, 210), 90f), new Rectangle(0, 0, Width, Height));

                Color c1 = (_checked) ? _selOutline : _outline;
                Color c2 = (_checked) ? _selColor : _color;

                e.Graphics.FillEllipse(new SolidBrush(c2), new Rectangle(4, 4, 23, 23));
                e.Graphics.DrawEllipse(new Pen(c1), new Rectangle(4, 4, 23, 23));
                if (Focused)
                {
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(0, 0, Width - 1, Height - 1));
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(1, 1, Width - 3, Height - 3));
                }
            }
        }

        #region Properties

        public bool Checked
        {
            get { return _checked; }
            set { _checked = value; Invalidate(); }
        }

        public Color Color
        {
            get { return _color; }
            set { _color = value; Invalidate(); }
        }

        public string GroupName
        {
            get { return _groupName; }
            set { _groupName = value; }
        }

        public Color OutlineColor
        {
            get { return _outline; }
            set { _outline = value; Invalidate(); }
        }

        public Color SelectedColor
        {
            get { return _selColor; }
            set { _selColor = value; Invalidate(); }
        }

        public Color SelectedOutlineColor
        {
            get { return _selOutline; }
            set { _selOutline = value; Invalidate(); }
        }

        public ISettings Settings
        {
            get { return _settings; }
        }

        public bool ShowBackground
        {
            get { return _showBackground; }
            set { _showBackground = value; Invalidate(); }
        }

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        #endregion

        #region Moving/Sizing

        private bool _move;
        private Point _sizeRef;

        private void GlideRadioButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (_move)
            {
                this.Left += e.X - _sizeRef.X;
                this.Top += e.Y - _sizeRef.Y;
                return;
            }
            _sizeRef = new Point(e.X, e.Y);
        }

        private void GlideRadioButton_MouseDown(object sender, MouseEventArgs e)
        {
            _move = true;
            this.Cursor = Cursors.SizeAll;
        }

        private void GlideRadioButton_MouseUp(object sender, MouseEventArgs e)
        {
            _move = false;
            this.Cursor = Cursors.Default;
        }

        #endregion

    }
}
